package test_model_database;

import static org.junit.Assert.*;

import java.sql.Connection;
import java.sql.Date;

import junit.framework.TestCase;

import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

import com.servlet.model.base.Area;
import com.servlet.model.base.Profile;
import com.servlet.model.base.Status;
import com.servlet.model.database.Database;

public class DatabaseTest extends TestCase {
	private String url;
	private String user;
	private String passwd;
	private Connection conn;
	private Database d;
	private int hcode = ("91400franceorsay").hashCode();
	private String sql = "INSERT INTO area VALUES (" + hcode + ", 91400, 'france', 'orsay');";
	private String dsql = "DELETE FROM area WHERE hcode = " + hcode;
	private String email_false = "jialin.liu@u-psud.fr";
	private String email_true = "Dabia.Garnier@propig.m";
	Profile p = new Profile("Dabia.Garnier@propig.m", "dabia", "garnier", Date.valueOf("1993-01-01"),
			new Status("Recherche un emploi"), "Dafina_Blanc.jpg", new Area(94828, "Pays-Bas", "paris"), 8, 15);
	
	@Before
	public void setUp() throws Exception {
		super.setUp();
		url = "jdbc:postgresql://localhost:5432/propig";
		user = "postgres";
		passwd = "postgres";
		d = new Database(url, user, passwd);
		d.connect();
	}

	@After
	public void tearDown() throws Exception {
		d.deconnect();
		d = null;
		url = null;
		user = null;
		passwd = null;
	}

	@Test
	public void testConnect() {
		d.deconnect();
		Assert.assertEquals(true, d.connect());
	}

	@Test
	public void testDeconnect() {
		Assert.assertEquals(true, d.deconnect());
	}

	@Test
	public void testSelectSQL() {
		d.insertSql(sql);
		String ssql = "SELECT * FROM area WHERE hcode=" + hcode;
		Assert.assertEquals(hcode + "91400franceorsay", d.selectSQL(ssql) );
		d.deleteSql(dsql);
	}

	@Test
	public void testInsertSql() {
		d.deleteSql(dsql);
		Assert.assertEquals(true, d.insertSql(sql));
		d.deleteSql(sql);
	}

	@Test
	public void testDeleteSql() {
		d.deleteSql(dsql);
		Assert.assertEquals(true, d.deleteSql(dsql));
	}

	@Test
	public void testExistPasswd() {
		Assert.assertEquals(false, d.existPasswd("passwd"));
	}

	@Test
	public void testExistEmail() {
		Assert.assertEquals(false, d.existEmail(email_false));
		Assert.assertEquals(true, d.existEmail(email_true));
	}

	@Test
	public void testFindEmailBySpecialite() {
		Assert.assertNull(d.findEmailBySpecialite("art"));
	}
}
